﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using Business;
namespace ServicesDirectory.UserControl
{
    /// <summary>
    /// 
    /// </summary>
    public partial class OrganisationDetail5 : System.Web.UI.UserControl
    {
        /// <summary>
        /// Occurs when [set data detail5].
        /// </summary>
        public event EventHandler SetDataDetail5;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(SetDataDetail5!=null)
                {
                    SetDataDetail5(sender, e);
                }
            }
        }

        /// <summary>
        /// Handles the ItemDataBound event of the RptSupportingMaterial control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        protected void RptSupportingMaterial_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lbluser = e.Item.FindControl("lblAddedBy") as Label;
            if (lbluser != null)
            {
                var user = new UserBusiness();
                var getUser = user.GetUserById(lbluser.Text);
                if (getUser != null)
                    lbluser.Text = getUser.Account;
            }
            var lblDate = e.Item.FindControl("lblAddedDate") as Label;
            if (lblDate != null)
            {
                lblDate.Text = DateTime.Parse(lblDate.Text,CultureInfo.InvariantCulture).ToString("dd-MM-yyyy",CultureInfo.InvariantCulture);
            }
        }

        /// <summary>
        /// Binds the data.
        /// </summary>
        /// <param name="organisationId">The organisation id.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        public void BindData(string organisationId, bool isActive)
        {
            var lstMaterial = SupportMaterialBusiness.Instance.ListSupportMaterial(organisationId, isActive);
            if (lstMaterial != null)
            {
                lblTotalPage.Text = lstMaterial.Count % 15 == 0 ? (lstMaterial.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstMaterial.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptSupportingMaterial.DataSource = SupportMaterialBusiness.Instance.PagingSupportingMaterial(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstMaterial);
                rptSupportingMaterial.DataBind();
            }

        }

        /// <summary>
        /// RPTs the supporting material item command.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="e">The <see cref="CommandEventArgs" /> instance containing the event data.</param>
        protected void RptSupportingMaterialItemCommand(object source, CommandEventArgs e)
        {
            if (e.CommandName == "EditSupporting")
            {
                Response.Redirect("ManagerSupportingMaterial.aspx?id=" + e.CommandArgument);
            }
        }
    }
}